package em.ctrl;

import base.ctrl.BaseController;
import base.jfinal.anatation.RouteBind;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;
import java.util.List;

@RouteBind(path="RoleManage")
public class RoleManageCtrl extends BaseController
{
  public void getModuleTree()
  {
    List Modules = Db.find("SELECT MenuId 'id',`Name` 'name',IFNULL(PId,0) 'pId',MenuId 'mid','m' AS 'type' FROM em_menu ORDER BY PId,orderCode");
    List Buttons = Db.find("SELECT 3000+ID 'id',`Name` 'name',MenuNo 'pId','b' AS 'type' FROM em_privilege where type=0");
    Modules.addAll(Buttons);
    
    JSONArray ModuleTree=new JSONArray(Modules);

    JSONObject jsonObject=new JSONObject();
    jsonObject.put("id",0);
    jsonObject.put("name","工程机械管理平台");
    jsonObject.put("open",true);
    ModuleTree.add(0,jsonObject);

    renderJson(ModuleTree);
  }
  public void getRoleInfoById() {
    Record Role = Db.findFirst("SELECT RoleId,`Name`,orgId FROM em_role WHERE RoleId=?", new Object[] { getParaToInt("RoleId") });
    List Privileges = Db.find("SELECT 3000+PrivilegeMapValues 'id' FROM em_privilege_role_map  WHERE PrivilegeMapType='b' AND PrivilegeMapRoleID=?", new Object[] { getParaToInt("RoleId") });
    JSONObject RoleJson = JSONObject.parseObject(Role.toJson());
    RoleJson.put("privileges", Privileges);
    renderJson(RoleJson);
  }

  public void SaveRoleInfo() {
    String privileges = getPara("Privileges");
    int RoleId = getParaToInt("RoleId").intValue();
    int OrgId = getParaToInt("OrgId").intValue();
    String Name = getPara("Name");
    JSONArray Ps = JSONArray.parseArray(privileges);
    Object[][] paras = new Object[Ps.size()][3];

    if (RoleId == 0){
      int listcount = Db.find("Select RoleId From em_role Where Name=? And orgId=?", new Object[] { Name, Integer.valueOf(OrgId) }).size();
      if (listcount > 0) {
        renderJson("{\"err\":1}");
        return;
      }
      Record Role = new Record().set("Name", Name).set("orgId", Integer.valueOf(OrgId)).set("createDate", new Date()).set("createBy",getParaToInt("GlobalAccId"));
      Db.save("em_role", "RoleId", Role);
      RoleId = Db.findFirst("Select RoleId From em_role Where Name=? And orgId=?", new Object[] { Name, Integer.valueOf(OrgId) }).getInt("RoleId").intValue();
    }
    else
    {
      Db.update("UPDATE em_role SET `Name`=?,updateBy=?,updateDate=? WHERE RoleId=?", new Object[] { Name, getParaToInt("GlobalAccId"), new Date(), Integer.valueOf(RoleId) });
      Db.deleteById("em_privilege_role_map", "PrivilegeMapRoleId", Integer.valueOf(RoleId));
    }
    for (int i = 0; i < Ps.size(); i++) {
      JSONObject privilege = Ps.getJSONObject(i);
      Object[] PR = { Integer.valueOf(privilege.getIntValue("id")), privilege.getString("type"), Integer.valueOf(RoleId) };
      paras[i] = PR;
    }
    Db.batch("INSERT INTO em_privilege_role_map(PrivilegeMapValues,PrivilegeMapType,PrivilegeMapRoleID) VALUES(?,?,?)", paras, 300);
    renderJson("{\"err\":0}");
  }
  public void getOrgList() {
    List OrgList = Db.find("SELECT OrgId,`Name` FROM em_org");
    renderJson(OrgList);
  }
  public void getRole2Select() {
    List RoleList = Db.find("SELECT RoleId,`Name`,orgId FROM em_role");
    renderJson(RoleList);
  }
}